package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.entity.*;
import yf.mall.service.*;

import java.util.List;

/** que
 *  直播
 */
@RestController
@RequestMapping("api/live")
public class ApiLiveController {
    @Autowired
    private LiveService liveService;
    @Autowired
    private MallGoodsService mallGoodsService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private SaleTypeInfoService saleTypeInfoService;
    @Autowired
    private UserService userService;

    /** que
     * 获取所有直播列表
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginUserId Long userId,int page, int limit){
        PageHelper.startPage(page, limit);
        List<LiveEntity> list = liveService.getList();
        //获取店铺信息
        if (list != null && list.size() > 0){
            for (LiveEntity liveEntity : list) {
                if (liveEntity.getUserId() != null){
                    UserEntity userEntity = userService.get(liveEntity.getUserId());
                    ShopEntity shopEntity = shopService.findByUserId(userEntity.getId());
                    if (shopEntity != null){
                        liveEntity.setShopEntity(shopEntity);
                    }
                }
                //查看直播是否结束
                long nowTime = System.currentTimeMillis();
                if (nowTime > liveEntity.getStart().getTime() && nowTime < liveEntity.getEnd().getTime()){
                    liveEntity.setFlag(true);
                }else {
                    liveEntity.setFlag(false);
                }
            }
        }
        return R.ok(list);
    }

    /** que
     *  查看直播
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id")Long id){
        LiveEntity liveEntity = liveService.get(id);
        if (liveEntity == null){
            throw new RRException("该直播不存在");
        }
        //获取直播对应的产品
        getGoods(liveEntity);
        return R.ok(liveEntity);
    }

    //获取直播对应的产品
    private void getGoods(LiveEntity liveEntity) {
        List<Long> goodsIds = liveService.findByLiveId(liveEntity.getId());

        if (goodsIds != null && goodsIds.size() > 0) {
            for (Long goodsId : goodsIds) {
                MallGoodsEntity mallGoodsEntity = mallGoodsService.get(goodsId);
                //获取售卖类型详情
                SaleTypeInfoEntity saleTypeInfoEntity = saleTypeInfoService.findByGoodsId(mallGoodsEntity.getId());
                if (saleTypeInfoEntity != null){
                    mallGoodsEntity.setAct(saleTypeInfoEntity);
                }
                liveEntity.getGoods().add(mallGoodsEntity);
            }
            //获取店铺信息
            MallGoodsEntity mallGoodsEntity = mallGoodsService.get(goodsIds.get(0));
            if (mallGoodsEntity.getUserId() != null){
                ShopEntity shopEntity = shopService.findByUserId(mallGoodsEntity.getUserId());
                liveEntity.setShopEntity(shopEntity);
            }
        }
    }
}
